//
//  BSLoginViewController.m
//  BaiSi
//
//  Created by Sycamore on 16/4/28.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSLoginViewController.h"
#import "BSLoginRegisterView.h"
#import "BSFastLoginView.h"

@interface BSLoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *middleView;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *middleViewLeading;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation BSLoginViewController
- (IBAction)login {
    
    //用了约束，修改frame就只能用约束改了
    _loginButton.selected = !_loginButton.selected;
    _middleViewLeading.constant = _loginButton.selected ? -BSScreenWidth : 0 ;
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
    
}
- (IBAction)cancel {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


//1 一个控件从xib加载，一定需要再次确定其尺寸和位置
//2 在viewDiDLoad方法中设置控制器view的子控件，约束不会生效，要在viewDidLayoutsubviews设置子控件的位置尺寸
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    BSLoginRegisterView *loginView = [BSLoginRegisterView loginView];
    [_middleView addSubview:loginView];
    
    BSLoginRegisterView *registerView = [BSLoginRegisterView registerView];
    [_middleView addSubview:registerView];
    
    BSFastLoginView *fastLoginView = [BSFastLoginView fastLoginView];
    [_bottomView addSubview:fastLoginView];
}


//要想子控件的约束生效，需要在viewDidLayoutsubViews里面确定子控件的位置和尺寸
- (void)viewDidLayoutSubviews
{
    BSLoginRegisterView *loginView = _middleView.subviews[0];
    loginView.frame = CGRectMake(0, 0, BSScreenWidth, _middleView.bs_height);
    
    
    BSLoginRegisterView *registerView = _middleView.subviews[1];
    registerView.frame = CGRectMake(BSScreenWidth, 0, BSScreenWidth, _middleView.bs_height);
    
    BSFastLoginView *fastLoginView = _bottomView.subviews[0];
    fastLoginView.frame = CGRectMake(0, 0, BSScreenWidth, _bottomView.bs_height);
}

@end
